
package com.jf.cloud.multishop.controller.app;

import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.biz.feign.NotifyFeignClient;
import com.jf.cloud.common.constant.SendTypeEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.multishop.dto.ShopDetailDTO;
import com.jf.cloud.multishop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 店铺操作
 *
 * @author zz
 * @date 2020-12-05 15:50:25
 */
@RestController("appShopAuditingController")
@RequestMapping("/ua/shop_auditing")
@Tag(name = "app-店铺申请")
public class ShopAuditingController {

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private NotifyFeignClient notifyFeignClient;

    @PostMapping("/apply")
    @Operation(summary = "申请店铺" , description = "申请店铺")
    public ServerResponseEntity<Void> apply(@Valid @RequestBody ShopDetailDTO shopDetailDTO) {
        shopDetailService.applyShop(shopDetailDTO);
        checkCode(shopDetailDTO);
        shopDetailService.removeCacheByShopId(shopDetailDTO.getShopId());
        return ServerResponseEntity.success();
    }

    /**
     * 校验验证码
     * @param shopDetailDTO
     * @return
     */
    private void checkCode(ShopDetailDTO shopDetailDTO) {
        // 手机号验证码
        if (StrUtil.isNotBlank(shopDetailDTO.getPhone())) {
            ServerResponseEntity<Boolean> sendNotifyResponse = notifyFeignClient.checkValidCode(shopDetailDTO.getPhone(), shopDetailDTO.getCode(), SendTypeEnum.VALID);
            Boolean data = sendNotifyResponse.getData();
            if (Objects.equals(sendNotifyResponse.getCode(), ResponseEnum.OK.value()) && Objects.nonNull(data) && !data) {
                throw new LuckException("验证码有误或已过期");
            }
        }
        //邮箱验证码
    }
}
